import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/admin',
    component: () => import('@/layouts/AdminLayout.vue'),
    redirect: '/admin/home',
    children: [
      {
        path: 'home',
        name: 'AdminIndex',
        component: () => import('@/views/admin/HomeView.vue'),
        meta: { title: '后台首页' }
      },
      {
        path: 'users',
        name: 'AdminUserList',
        component: () => import('@/views/admin/user/UserList.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'user/add',
        name: 'AdminUserAdd',
        component: () => import('@/views/admin/user/UserAdd.vue'),
        meta: { title: '添加用户' }
      },
      {
        path: 'user/update/:id',
        name: 'AdminUserUpdate',
        component: () => import('@/views/admin/user/UserUpdate.vue'),
        meta: { title: '修改用户' }
      },
      {
        path: 'goods',
        name: 'AdminGoodsList',
        component: () => import('@/views/admin/goods/GoodsList.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'goods/add',
        name: 'AdminGoodsAdd',
        component: () => import('@/views/admin/goods/GoodsAdd.vue'),
        meta: { title: '添加商品' }
      },
      {
        path: 'goods/update/:id',
        name: 'AdminGoodsUpdate',
        component: () => import('@/views/admin/goods/GoodsUpdate.vue'),
        meta: { title: '修改商品' }
      },
      {
        path: 'orders',
        name: 'OrdersList',
        component: () => import('@/views/admin/orders/OrdersList.vue'),
        meta: {
          title: '订单列表',
          icon: 'List'
        }
      },
      {
        path: 'orders/detail/:id/:orderNo',
        name: 'OrderDetail',
        component: () => import('@/views/admin/orders/OrderDetail.vue'),
        meta: {
          title: '订单详情',
          icon: 'Document',
          hidden: true
        }
      },
      {
        path: 'banner',
        name: 'BannerList',
        component: () => import('@/views/admin/banner/BannerList.vue'),
        meta: {
          title: '轮播图管理',
          icon: 'Picture'
        }
      }
    ]
  },
  {
    path: '/home',
    name: 'HomeIndex',
    component: () => import('@/views/home/HomeView.vue'),
    meta: { title: '前台首页' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/LoginView.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/registerView.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/',
    redirect: '/admin'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '电子商城管理系统'
  
  // 获取token
  const token = localStorage.getItem('token')
  
  // 白名单路径，不需要登录就可以访问
  const whiteList = ['/login', '/register']
  
  if (token) {
    // 已登录状态
    if (to.path === '/login') {
      // 如果已登录，访问登录页则重定向到首页
      next('/')
    } else {
      // 其他页面正常访问
      next()
    }
  } else {
    // 未登录状态
    if (whiteList.includes(to.path)) {
      // 在白名单中，可以直接访问
      next()
    } else {
      // 不在白名单中，重定向到登录页
      next(`/login?redirect=${to.path}`)
    }
  }
})

export default router